from peewee import BigIntegerField, CharField, DateTimeField, BigAutoField, BooleanField, SmallIntegerField, FloatField, \
    DateField
from common.model.abstrct import AbstractModelWithID, IntEnumField, AbstractModel
from enums.client_user_enum import UserCouponStatusEnum, UserGenderTypeEnum


class ClientUser(AbstractModel):

    class Meta:
        table_name = "tb_client_user"
        indexes = (
            (("tenant_id", "email"), False),
            (("tenant_id", "phone"), False),
        )
        auto_increment_base = 100_000_000_000

    tenant_id = BigIntegerField(help_text="租户id")
    user_id = BigAutoField(primary_key=True, help_text="用户id")
    username = CharField(help_text="用户名", max_length=32, null=True)
    alias = CharField(max_length=16, help_text="别名", null=True)
    password = CharField(max_length=128, help_text="密码")
    email = CharField(max_length=64, help_text="用户邮箱", null=True)
    phone = CharField(max_length=16, help_text="用户手机号", null=True)
    country_code = CharField(max_length=4, help_text="手机号国家码", default="86")
    avatar = CharField(max_length=128, help_text="头像", null=True)
    description = CharField(255, help_text="用户简介", null=True)
    gender = IntEnumField(help_text="性别", choices=UserGenderTypeEnum, default=UserGenderTypeEnum.unknown)
    birthday = DateField(help_text="生日", null=True)
    invite_code = CharField(max_length=10, help_text="用户邀请码")
    invited_code = CharField(max_length=10, help_text="用户受邀码", null=True)
    is_vip = BooleanField(default=False, help_text="是否是会员")
    level = SmallIntegerField(help_text="会员等级", default=0)


class VipConfigInfo(AbstractModel):

    class Meta:
        table_name = "tb_client_vip_config"
        indexes = (
            (("tenant_id", ), False),
        )

    tenant_id = BigIntegerField()
    vip_level = SmallIntegerField(help_text="会员等级")
    discount = FloatField(default=10, help_text="会员购物折扣,10表示不打折")
    point_rate = FloatField(default=1, help_text="积分转换率，默认1:1转换")


class MiddleUserCoupon(AbstractModelWithID):

    class Meta:
        table_name = "tb_middle_user_coupon"
        indexes = (
            (("user_id", ), False),  # 查当前用户的券
            (("coupon_id", ), False),  #?这个拿来干什么用呢
            (("coupon_code", ), False),  # 使用时查券是否存在
        )

    user_id = BigIntegerField(help_text="用户id")
    coupon_id = BigIntegerField(help_text="优惠券id")
    coupon_code = CharField(help_text="优惠券code")
    start_datetime = DateTimeField(help_text="优惠券可以使用的时间")
    end_datetime = DateTimeField(help_text="优惠券可以使用的时间")
    status = IntEnumField(help_text='优惠券状态', choices=UserCouponStatusEnum)